﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class Oc_PreViewPanelController : OC_PanelController
{
    Animation m_3DModelsAni;
    int m_SlectButtonNum = 0;

    public GameObject[] m_Buttons;
    public Animation m_CharMoveAni;

    public GameObject m_ZoomInButton;
    public GameObject m_ZoomOutButton;

    static public MemberModel m_SelectModel;

    void GetAnimation()
    {
        CharacterInfoScene tScene = CharacterInfoScene.Instance as CharacterInfoScene;
        m_3DModelsAni = tScene.m_3DModel.GetComponent("Animation") as Animation;
        m_SelectModel = tScene.m_SelectMember;
        //WearItem();
    }

    //void WearItem()
    //{
    //    BattleCharInfo charInfo = m_3DModelsAni.GetComponent("BattleCharInfo") as BattleCharInfo;        
        
    //    AttachWeapon(charInfo.gameObject, charInfo);
    //}

    
    //static Dictionary<string, GameObject> weaponPrefabDic = new Dictionary<string, GameObject>();
     
    //public void AttachWeapon(GameObject _parent, BattleCharInfo charInfo)
    //{

    //    int[] itemModelId = new int[2];

    //    if (m_SelectModel.rightItem != null)
    //        itemModelId[0] = m_SelectModel.rightItem.itemInfo.modelID;
 

    //    if (m_SelectModel.leftItem != null)
    //        itemModelId[1] = m_SelectModel.leftItem.itemInfo.modelID;
     
    //    m_WeaponModels = BattleResManager.AttachWeapon(_parent, charInfo, itemModelId);		
    //}

    void OnEnable()
    {
        m_ZoomInButton.SetActive(true);
        m_ZoomOutButton.SetActive(false); 

        GetAnimation();
        SetButtons(0);     
    }

    void OnDisable()
    {
        if (m_3DModelsAni != null)
        {
            m_3DModelsAni["lobby01"].clip.wrapMode = WrapMode.Loop;
            m_3DModelsAni.CrossFade("lobby01");
            iTween.RotateTo(m_3DModelsAni.gameObject, iTween.Hash("islocal", true, "y", 0, "time", 0.5f));
        }
        CharacterInfoScene tScene = CharacterInfoScene.Instance as CharacterInfoScene;
        // 다른 인원 선택시 인포 리스트 초기화        
        OC_InfoUIWithScroll.instance.m_SelectListNum = -1;
        OC_InfoUIWithScroll.instance.MoveList();
    }

    void SetButtons(int num)
    {
        m_SlectButtonNum = num;

        for(int i = 0; i < m_Buttons.Length ; i++)
        {
            if(i == m_SlectButtonNum)
            {
                m_Buttons[i].transform.Find("on").gameObject.SetActive(true);
                m_Buttons[i].transform.Find("off").gameObject.SetActive(false);
            }
            else
            {
                m_Buttons[i].transform.Find("on").gameObject.SetActive(false);
                m_Buttons[i].transform.Find("off").gameObject.SetActive(true);
            }
        }

        SetAnimation();
    }

    void SetAnimation()
    {
        switch(m_SlectButtonNum)
        {
            case 0:
                m_3DModelsAni.CrossFade("idle01", 0.5f);
                break;
            case 1:
                m_3DModelsAni.CrossFade("attack01", 0.5f);
                m_3DModelsAni.CrossFadeQueued("idle01", 0.5f);
                break;
            case 2:
                m_3DModelsAni.CrossFade("skill01");
                m_3DModelsAni.CrossFadeQueued("idle01", 0.5f);
                break;
            case 3:
                m_3DModelsAni.CrossFade("victory01");
                m_3DModelsAni.CrossFadeQueued("idle01", 0.5f);
                break;
        }        
    }

    public override void UpdateTouch()       // 터치 입력 받는 함수. ButtonTouch 외에 다른 작업할경우 오버라이드해서 구현 사용
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray;
            RaycastHit rayHit;

            ray = m_Cam.ScreenPointToRay(Input.mousePosition);

            if (Raycast(ray, out rayHit))
            {
                m_PretouchOBJ = rayHit.collider.gameObject;
            }

            m_SaveTouchDown.Set(Input.mousePosition.x, Input.mousePosition.y);
        }

        if (Input.GetMouseButton(0))
        {
            if (!m_IsDrag)
            {
                if (System.Math.Abs(Input.mousePosition.x - m_SaveTouchDown.x) > m_DragGap
                    || System.Math.Abs(Input.mousePosition.y - m_SaveTouchDown.y) > m_DragGap)
                {
                    m_IsDrag = true;
                }
                else
                {
                    m_IsDrag = false;
                }
            }

            if(m_IsDrag)    //드래그 일때 케릭터 회전
            {
                Vector3 lotation = m_3DModelsAni.transform.localRotation.eulerAngles;
                lotation.y = m_SaveTouchDown.x - Input.mousePosition.x;
                m_3DModelsAni.transform.localRotation = Quaternion.Euler(lotation);
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            Ray ray;
            RaycastHit rayHit;

            ray = m_Cam.ScreenPointToRay(Input.mousePosition);

            if (Raycast(ray, out rayHit))
            {
                if (m_PretouchOBJ != rayHit.collider.gameObject)
                    return;
            }

            if (!m_IsDrag && rayHit.collider != null)
            {
                ButtonTouch(rayHit.collider);
            }

            m_IsDrag = false;
            m_PretouchOBJ = null;
        }
    }

    public override void ButtonTouch(Collider collider)  // 버튼 터치 (터치 시작 끝 같은 오브젝트일때 터치 끝 시점에 호출)
    {
        switch (collider.gameObject.name)
        {
            case "Button_MotionList_01":
                {
                    SoundController.PlaySound("UI_button");
                    SetButtons(0);
                }
                break;
            case "Button_MotionList_02":
                {
                    SoundController.PlaySound("UI_button");
                    SetButtons(1);
                }
                break;
            case "Button_MotionList_03":
                {
                    SoundController.PlaySound("UI_button");
                    SetButtons(2);
                }
                break;
            case "Button_MotionList_04":
                {
                    SoundController.PlaySound("UI_button");
                    SetButtons(3);
                }
                break;
            case "Button_ZoomIn":
                {
                    m_CharMoveAni.CrossFade("char_View_expansion", 0.5f);
                    m_ZoomInButton.SetActive(false);  
                    m_ZoomOutButton.SetActive(true);              
                }
                break;
            case "Button_ZoomOut":
                {
                    m_CharMoveAni.CrossFade("char_View_reduction", 0.5f);
                    m_ZoomInButton.SetActive(true);
                    m_ZoomOutButton.SetActive(false); 
                }
                break; 
        }
    }       
}